@echo off

rem cmake -B/path/to/my/build/folder -H/path/to/my/source/folder

cls

rem -----------------------------------------------------------------------------

set MODE=Debug
set ADDRESSMODEL=32
set ACTION=Stop
set ACTION=Launch
set ACTION=Build

rem -----------------------------------------------------------------------------

if "%NAME_PRODUCT%"     == ""   for %%a in (.) do set NAME_PRODUCT=%%~na
if "%SOURCE_DIRECTORY%" == ""   set SOURCE_DIRECTORY=%~dp0
if "%GENERATOR%"        == ""   set GENERATOR=Visual Studio 11
if "%TOOLSET%"          == ""   set TOOLSET=v120_CTP_Nov2012
if "%MODE%"             == ""   set MODE=Debug
if "%ADDRESSMODEL%"     == ""   set ADDRESSMODEL=32
if "%ACTION%"           == ""   set ACTION=stop
if "%ADDRESSMODEL%"     == "64" set SUFFIX= Win64
if "%BUILDDIR%"         == ""   set BUILDDIR=%MODE%%ADDRESSMODEL%

echo PROJECT NAME ......... %NAME_PRODUCT%
echo SOURCE DIRECTORY ..... %SOURCE_DIRECTORY%
echo IDE .................. %GENERATOR%%SUFFIX%
echo TOOLSET .............. %TOOLSET%
echo BUILD TYPE ........... %MODE%
echo ADDRESS MODEL ........ %ADDRESSMODEL%
echo ACTION ............... %ACTION%

rem -----------------------------------------------------------------------------

if not exist build ( mkdir build )
cd build

set BUILDDIR=%MODE%%ADDRESSMODEL%
if exist %BUILDDIR% ( rmdir /s /q %BUILDDIR% )
mkdir %BUILDDIR%
cd %BUILDDIR%

cmake  -G "%GENERATOR%%SUFFIX%" ^
       -T "%TOOLSET%" ^
       -DCMAKE_C_COMPILER_WORKS=true ^
       -DCMAKE_CXX_COMPILER_WORKS=true ^
       -DCMAKE_BUILD_TYPE=%MODE% ^
       -DMY_AMODEL=%ADDRESSMODEL% ^
       -DUNIT_TEST_MODE=%UNITTESTMODE% ^
        %SOURCE_DIRECTORY%

rem -----------------------------------------------------------------------------

echo [ %ACTION% ] ...

if "%ACTION%" == "stop" exit/b

set vsvars1=C:\"Program Files (x86)\Microsoft Visual Studio 11.0"\Common7\Tools\vsvars32.bat
set vsvars2=C:\"Program Files\Microsoft Visual Studio 11.0"\Common7\Tools\vsvars32.bat
if exist %vsvars1% ( call %vsvars1% )
if exist %vsvars2% ( call %vsvars2% )

if "%ACTION%" == "launch" ( 
    devenv %NAME_PRODUCT%.sln 
) ELSE ( 
    devenv %NAME_PRODUCT%.sln /%ACTION% %MODE%
    if exist INSTALL.vcxproj (
        devenv %NAME_PRODUCT%.sln /%ACTION% %MODE% /project INSTALL.vcxproj /projectconfig %MODE%
    )
)

cd ..
cd ..

exit/b
       

